package com.david.zhufengfm.widgets;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.david.zhufengfm.R;
import com.david.zhufengfm.utils.DimensionUtil;

/**
 * Created by David on 2015/10/22.
 * qq:978944820
 */

/**
 * 用于发现模块中，推荐栏目中"精品听单"的条目展现
 */
public class ColumnsItemView extends RelativeLayout{
    /**
     * 听单的图标
     */
    private ImageView imgIcon;
    /**
     * 听单标题
     */
    private TextView txtTitle;
    /**
     * 听单子标题
     */
    private TextView subTitle;

    /**
     * 最右侧的箭头
     */
    private ImageView imgArrow;
    /**
     * 底部的线段
     */
    private ImageView imgLine;



    /**
     * 在代码中使用new SpecialItemView的时候，调用这个构造方法
     * @param context
     */
    public ColumnsItemView(Context context) {
        super(context);
        init(context,null);
    }

    /**
     *在布局xml中，使用这个控件的时候，使用的构造
     * @param context
     * @param attrs
     */
    public ColumnsItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ColumnsItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private void init(Context context,AttributeSet attrs){
        //初始化控件
        imgIcon=new ImageView(context);
        //手写属性
        //LayoutParams代表在布局中的android:layout_xxx属性
        //控件要添加到哪一个容器中，就用哪一个容器的LayoutParams
        LayoutParams lp=
                new LayoutParams(DimensionUtil.dp2px(context,70),
                        DimensionUtil.dp2px(context,70));
        //ImageView 垂直居中
        //相当于layout_centerVertical="true"

        lp.addRule(CENTER_VERTICAL);

        imgIcon.setLayoutParams(lp);

        //设置ImageView 的ID，values/ids.xml
        imgIcon.setId(R.id.columns_item_icon);
        imgIcon.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imgIcon.setImageResource(R.mipmap.ic_launcher);
        addView(imgIcon);
        //----------------------------------------------------------------------------
        //标题部分
        txtTitle=new TextView(context);
        lp=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置TextView与ImageView顶部对齐
        lp.addRule(ALIGN_TOP,R.id.columns_item_icon);
        lp.addRule(RIGHT_OF, R.id.columns_item_icon);
        lp.leftMargin=DimensionUtil.dp2px(context,20);//单位像素，需要进行机型适配
        txtTitle.setLayoutParams(lp);
        txtTitle.setText("标题");
        txtTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        txtTitle.setTextColor(Color.BLACK);
        txtTitle.setId(R.id.columns_item_title);
        txtTitle.setSingleLine();;
        addView(txtTitle);
        //-----------------------------------------------------------------
        subTitle=new TextView(context);
        lp=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        lp.addRule(ALIGN_LEFT, R.id.columns_item_title);
        lp.addRule(ALIGN_BOTTOM, R.id.columns_item_icon);
//        lp.addRule(BELOW, R.id.special_item_title);
        subTitle.setLayoutParams(lp);

        subTitle.setText("SubTitle");
        subTitle.setId(R.id.columns_item_subtitle);
        //单行
        subTitle.setSingleLine();
        subTitle.setTextSize(14);
        addView(subTitle);

        //------------------------------------------------------------
        imgArrow=new ImageView(context);
        lp=new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        //容器右对齐
        lp.addRule(ALIGN_PARENT_RIGHT);
        lp.addRule(CENTER_VERTICAL);
        imgArrow.setLayoutParams(lp);
        imgArrow.setImageResource(R.mipmap.arrow_right);
        addView(imgArrow);
        //--------------------------------------------------------------
        imgLine=new ImageView(context);
        //显示1个像素高度的图片
        lp=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,1);
        //与title左对齐
        lp.addRule(ALIGN_LEFT,R.id.columns_item_title);
        lp.addRule(ALIGN_PARENT_RIGHT);
        //在数量的下边
        lp.addRule(BELOW,R.id.columns_item_subtitle);
        //指定间距
        lp.setMargins(0,DimensionUtil.dp2px(context,5),0,0);
        imgLine.setLayoutParams(lp);
        imgLine.setImageResource(R.drawable.shape_line);
        addView(imgLine);
    }

    /**
     * 设置title
     * @param str
     */
    public void setTitle(String str){
        if (str != null) {
            txtTitle.setText(str);
        }

    }

    /**
     * 设置subtitle
     * @param subTitleTxt
     */
    public void setSubTitle(String subTitleTxt){
        subTitle.setText(subTitleTxt);
    }


    /**
     * 设置底部的线是否显示
     * @param show
     */
    public void setShowBottomLine(boolean show){
        if (show){
            imgLine.setVisibility(VISIBLE);
        }else{
            //TODO 尝试
            imgLine.setVisibility(INVISIBLE);
        }
    }

    /**
     * 设置箭头样式
     * @param resId
     */
    public void setRightArrowResource(int resId){
        imgArrow.setImageResource(resId);
    }

    /**
     * 获取Icon控件
     * @return
     */
    public ImageView getImgIcon(){
        return imgIcon;
    }
}
